import SwiftUI

struct CultivationHomeView: View {
    @State private var tasks: [CultivationTask] = [
        CultivationTask(title: "晨练剑法", type: .technique, isCompleted: false),
        CultivationTask(title: "冥想打坐", type: .mind, isCompleted: true),
        CultivationTask(title: "内功修炼", type: .body, isCompleted: false),
        CultivationTask(title: "研读武功秘籍", type: .technique, isCompleted: false),
        CultivationTask(title: "体能训练", type: .body, isCompleted: true),
        CultivationTask(title: "静心冥想", type: .mind, isCompleted: false)
    ]
    // test1
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 20) {
                    dailyProgressView
                    taskListView
                }
                .padding()
            }
            .background(Color(.systemGroupedBackground))
            .navigationTitle("今日修炼")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        // Action to add new task
                    }) {
                        Image(systemName: "plus")
                    }
                }
            }
        }
    }

    private var dailyProgressView: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("今日进度")
                .font(.headline)
            HStack(spacing: 15) {
                ProgressRingView(progress: calculateProgress(for: .technique), color: .blue, title: "炼技")
                ProgressRingView(progress: calculateProgress(for: .body), color: .green, title: "炼体")
                ProgressRingView(progress: calculateProgress(for: .mind), color: .purple, title: "炼神")
            }
        }
        .padding()
        .background(Color(.systemBackground))
        .cornerRadius(15)
        .shadow(radius: 5)
    }

    private var taskListView: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("修炼任务")
                .font(.headline)
            ForEach($tasks) { $task in
                TaskRowView(task: $task)
            }
        }
        .padding()
        .background(Color(.systemBackground))
        .cornerRadius(15)
        .shadow(radius: 5)
    }

    private func calculateProgress(for type: CultivationType) -> Double {
        let typeTasks = tasks.filter { $0.type == type }
        let completedTasks = typeTasks.filter { $0.isCompleted }
        return Double(completedTasks.count) / Double(typeTasks.count)
    }
}

struct ProgressRingView: View {
    let progress: Double
    let color: Color
    let title: String

    var body: some View {
        VStack {
            ZStack {
                Circle()
                    .stroke(color.opacity(0.2), lineWidth: 10)
                Circle()
                    .trim(from: 0, to: CGFloat(progress))
                    .stroke(color, style: StrokeStyle(lineWidth: 10, lineCap: .round))
                    .rotationEffect(.degrees(-90))
                Text("\(Int(progress * 100))%")
                    .font(.system(size: 12, weight: .bold, design: .rounded))
            }
            .frame(width: 60, height: 60)
            Text(title)
                .font(.caption)
                .foregroundColor(.secondary)
        }
    }
}

struct TaskRowView: View {
    @Binding var task: CultivationTask

    var body: some View {
        HStack {
            Image(systemName: task.isCompleted ? "checkmark.circle.fill" : "circle")
                .foregroundColor(task.isCompleted ? task.type.color : .secondary)
                .onTapGesture {
                    task.isCompleted.toggle()
                }
            VStack(alignment: .leading) {
                Text(task.title)
                    .strikethrough(task.isCompleted)
                Text(task.type.rawValue)
                    .font(.caption)
                    .foregroundColor(task.type.color)
            }
            Spacer()
        }
        .padding(.vertical, 8)
    }
}

struct CultivationTask: Identifiable {
    let id = UUID()
    let title: String
    let type: CultivationType
    var isCompleted: Bool
}

enum CultivationType: String {
    case technique = "炼技"
    case body = "炼体"
    case mind = "炼神"

    var color: Color {
        switch self {
        case .technique: return .blue
        case .body: return .green
        case .mind: return .purple
        }
    }
}

struct CultivationHomeView_Previews: PreviewProvider {
    static var previews: some View {
        CultivationHomeView()
    }
}

